package Ske

import (
	"github.com/PuerkitoBio/goquery"
	"fmt"
	"strings"
	"log"
	"io"
)

type InParaInfo []string;
/**
 * 数组去重 去空 去多余的空格
 */
func removeDuplicatesAndEmpty(a []string) (ret []string) {

	a_len := len(a)
	for i := 0; i < a_len; i++ {
		if (i > 0 && a[i-1] == a[i]) || len(a[i]) == 0 {
			continue
		}
		ret = append(ret, strings.TrimSpace(a[i]))
	}
	return
}
/**
 * 数组去重 去空 去多余的空格
 */
func ExampleScrape(w io.Writer) {
	var  inPara = InParaInfo{} ;
	doc, err := goquery.NewDocument("http://support.huaweicloud.com/api-dns/zh-cn_topic_0042318613.html")
	if err != nil {
		log.Fatal(err)
	}
	// Find the topic items
	fmt.Println("topic",doc.Find(".topictitle1").Text(),"\r\n")
	doc.Find(".section").Each(func(i int, s *goquery.Selection) {
		// For each item found, get the band and title
		//title := s.Text();
		var c = s.Find("h4").Text()
		fmt.Println(c)
		switch c{
		case "功能介绍":
			fmt.Println(s.Find("p").Text())
			fmt.Print("\r\n")
			break;
		case "URI":
			fmt.Println(s.Find("span").Text())
			s.Find(".tablenoborder").Each(func(i int, s *goquery.Selection) {
				caption := s.Find("table").Find("caption").Text()
				caption = strings.TrimSpace(caption)
				fmt.Println(caption)
				ops := s.Find("table").Find("thead").Find("tr").Text()
				inPara = (removeDuplicatesAndEmpty(strings.Split(ops,"\n")))
				fmt.Println(inPara)
				s.Find("tbody").Find("tr").Each(func(i int, selection *goquery.Selection) {
					column := removeDuplicatesAndEmpty(strings.Split(selection.Text(),"\n"))
					fmt.Println(column)
				})
			})
			fmt.Print("\r\n")

			break;
		case "请求":
			s.Find(".tablenoborder").Each(func(i int, s *goquery.Selection) {
				caption := s.Find("table").Find("caption").Text()
				caption = strings.TrimSpace(caption)
				fmt.Println(caption)
				ops := s.Find("table").Find("thead").Find("tr").Text()
				inPara = (removeDuplicatesAndEmpty(strings.Split(ops,"\n")))
				fmt.Println(inPara)
				s.Find("tbody").Find("tr").Each(func(i int, selection *goquery.Selection) {
					column := removeDuplicatesAndEmpty(strings.Split(selection.Text(),"\n"))
					fmt.Println(column)
				})
				fmt.Print("\r\n")
			})
			break
		case "响应":
			s.Find(".tablenoborder").Each(func(i int, s *goquery.Selection) {
				caption := s.Find("table").Find("caption").Text()
				caption = strings.TrimSpace(caption)

				ops := s.Find("table").Find("thead").Find("tr").Text()
				inPara = (removeDuplicatesAndEmpty(strings.Split(ops,"\n")))
				fmt.Println(inPara)
				s.Find("tbody").Find("tr").Each(func(i int, selection *goquery.Selection) {
					column := removeDuplicatesAndEmpty(strings.Split(selection.Text(),"\n"))
					fmt.Println(column)
				})
				fmt.Print("\r\n")
			})
			break
	}
	//fmt.Printf("Review %d: %s\n", i, c)
	})
}